var wxSortPickerView = require('../../../wxSortPickerView/wxSortPickerView.js');
var constract = require('../../../utils/constract.js');
var app = getApp();
Page({
  data:{
    keyword: ''
  },
  onLoad: function (options) {
    var that = this;
    var sort = options.sort;
    that.setData({
      sort: sort
    });
  },
  onShow: function () {
    var that = this;
    app.Verification(function (token) {
      wx.request({
        url: constract.findCollect,
        header: { token: token },
        data: { type: that.data.sort, keyword: that.data.keyword },
        success: function (res) {
          console.log(res);

          var data = res.data.data;
          that.setData({
            list: data
          });
          var total = 0;
          if (that.data.sort == 1){
            for (var i = 0; i < data.length; i++) {
              var obj = data[i];
              var len = obj.list.length;
              total += len;
            };
          }else{
            total = that.data.list.length;
          }
          that.setData({
            total: total
          });
          wxSortPickerView.init(data, that);
        }
      })
    })
    app.globalData.url = {
      url: '../../details/details',
    }
  },
  goDetail: function (e) {
    wx.navigateTo({
      url: app.globalData.url.url + '?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type + '&ouid=' + e.currentTarget.dataset.ouid
    });
  },
  search: function(e){
    var key = e.detail.value;
    var that = this;
    app.Verification(function (token) {
      wx.request({
        url: constract.findCollect,
        header: { token: token },
        data: { type: that.data.sort, keyword: key },
        success: function (res) {
          var data = res.data.data;
          that.setData({
            list: data
          });
          var total = 0;
          if (that.data.sort == 1) {
            for (var i = 0; i < data.length; i++) {
              var obj = data[i];
              var len = obj.list.length;
              total += len;
            };
          } else {
            total = that.data.list.length;
          }
          that.setData({
            total: total
          });
          wxSortPickerView.init(data, that);
        }
      })
    })
  },
  getSearch: function(e){
    this.setData({
      keyword: e.detail.value
    })
  }
})